import {setStore} from '@/util/store'
import {query} from "@/api/mail/admin/mail-set"
import {queryLabel} from "@/api/mail/admin/mail-label"
import {queryAll} from '@/api/common/Dictionaries'
import {userMessage} from '@/api/message'
import {getEmailFirst, setEmailFirst, getSocialFirst, setSocialFirst} from '@/api/automation'
import {queryDict,queryCountry} from '@/api/common/Dictionaries' // 查询字典接口
import {toNumber, removeChildren} from '@/util/util'
import {querySignList} from "@/api/mail/admin/mail-sign";
import initState from '@/store/state/common'
const common = {
  state:initState,
  actions: {
    set_firstEmail: ({commit}, params) => {
      return new Promise((resolve, reject) => {
        if (params.type == 1) {//获取
          getEmailFirst().then(res => {
            if (res.data.success) {
              commit('SET_FIRSTEMAIL', !res.data.data);
              resolve();
            }
          }).catch(error => {
            reject(error)
          })
        } else {//修改
          setEmailFirst().then(res => {
            if (res.data.success) {
              commit('SET_FIRSTEMAIL', false);
              resolve();
            }
          }).catch(error => {
            reject(error)
          })
        }

      })
    },
    // 获取客户信息
    get_customer: ({commit}) => {
      return new Promise((resolve, reject) => {
        queryDict().then(res => {
          res.data.data.forEach((value, index) => {
            switch (value.dictKey) {
              case '营业额':  // 营业额
                commit('SET_TURNOVER', res.data.data[index].children);
                break;
              case '公司类型':  // 公司类型
                commit('SET_COMPANY_TYPE', toNumber(res.data.data[index].children));
                break;
              case '行业分类':  // 行业分类
                commit('SET_INDUSTRY', removeChildren(res.data.data[index].children));
                break;
              case '社交网络':  // 社交网络
                commit('SET_SOCIAL', toNumber(res.data.data[index].children));
                break;
              case '雇员人数':  // 雇员数
                commit('SET_EMPLE', res.data.data[index].children);
                break;
            }
          })
          resolve();
        }).catch(error => {
          reject(error)
        })
      })
    },
    get_country: ({commit}) => { // 城市
      return new Promise((resolve, reject) => {
        queryCountry().then(res => {
          commit('SET_COUNTRY', res.data.data.records);
          resolve();
        }).catch(error => {
          reject(error)
        })
      })
    },
    set_firstSocial: ({commit}, params) => {
      return new Promise((resolve, reject) => {
        if (params.type == 1) {//获取
          getSocialFirst().then(res => {
            if (res.data.success) {
              commit('SET_FIRSTSOCIAL', !res.data.data);
              resolve();
            }
          }).catch(error => {
            reject(error)
          })
        } else {//修改
          setSocialFirst().then(res => {
            if (res.data.success) {
              commit('SET_FIRSTSOCIAL', false);
              resolve();
            }
          }).catch(error => {
            reject(error)
          })
        }

      })
    },
    get_common_units: ({commit}) => {
      return new Promise((resolve, reject) => {
        queryAll().then(res => {
          const result = res.data.data;
          commit('SET_UNITS', result);
          resolve();
        }).catch(error => {
          reject(error)
        })
      })
    },
    save_curentEmail: ({commit}, param) => {
      commit('SAVECURRENTEMAIL', param)
    },
    update_labelList: ({commit}) => {
      return new Promise((resolve, reject) => {
        queryLabel().then(res => {
          commit('SET_LABELLIST', res.data.data);
          resolve();
        }).catch(error => {
          reject(error)
        })
      })
    },
    update_emailList: ({commit}) => {
      return new Promise((resolve, reject) => {
        query(1).then(res => {
          commit('SET_EMAILLIST', res.data.data);
          resolve();
        }).catch(error => {
          reject(error)
        })
      })
    },
    // set_message:
    set_message: ({commit}) => {
      return new Promise((resolve, reject) => {
        userMessage(1, 5, '', 4, false).then(res => {
          commit('SET_MESSAGElIST', res.data.data.page.records);
          commit('SET_TOTAL', res.data.data.page.total);
          let obj = {
            newMailCount: res.data.data.newEmailToast.count,
            newSystemCount: res.data.data.systemToast.count,
            auditCount: res.data.data.auditToast.count,
            custimpCount: res.data.data.custimpToast.count
          }
          commit('SET_NEWCOUNT', obj)
          resolve();
        }).catch(error => {
          reject(error)
        })
      })
    },
    set_sign: ({commit}) => {
      return new Promise((resolve, reject) => {
        querySignList({type: 1}).then(res => {
          const result = res.data.data;
          let moSign = '';
          for (let i = 0; i < result.length; i++) {
            if (result[i].isDefault) {//默认签名
              moSign = result[i].htmlContent
            }
          }
          commit('SET_SIGNLIST', "<br><br><br>" + moSign);
          resolve();
        }).catch(error => {
          reject(error)
        })
      })
    },
  },
  mutations: {
    set_isExpire(state,type){
      state.isExpired = type;
      setStore({
        name: 'ISEXPIRED',
        content: state.isExpired,
        type: 'session'
      })
    },
    SET_OCEANFRAG(state, frag) {
      state.oceanFrag = frag;
      setStore({
        name: 'OCEANFRAG',
        content: state.oceanFrag,
        type: 'session'
      })
    },
    SET_PRODUCTFRAG(state, frag) {
      state.productFrag = frag;
      setStore({
        name: 'PRODUCTFRAG',
        content: state.productFrag,
        type: 'session'
      })
    },
    SET_CURRENTPRODUCTID(state, id) {
      state.currentProductID = id;
      setStore({
        name: 'CURRENTPRODUCTID',
        content: state.currentProductID,
        type: 'session'
      })
    },
    //收件箱
    SET_EMAILLISTTAB(state, param) {//type,list,id
      if (param.type == 1) {//插入数组
        state.emailListTab.list.push(param.row);
        state.emailListTab.current = param.row.id;
      } else if (param.type == 0) {//保存当前高亮
        state.emailListTab.current = param.current;
      } else if (param.type == 2) {//删除一个
        state.emailListTab.list = param.list;
        state.emailListTab.current = param.list[param.index - 1].id
      }
      setStore({
        name: 'EMAILLISTTAB',
        content: state.emailListTab,
        type: 'session'
      })
    },
    //待处理邮件
    SET_EMAILLISTPENDINGTAB(state, param) {//type,list,id
      if (param.type == 1) {//插入数组
        state.emailListPendingTab.list.push(param.row);
        state.emailListPendingTab.current = param.row.id;
      } else if (param.type == 0) {//保存当前高亮
        state.emailListPendingTab.current = param.current;
      } else if (param.type == 2) {//删除一个
        state.emailListPendingTab.list = param.list;
        state.emailListPendingTab.current = param.list[param.index - 1].id
      }
      setStore({
        name: 'EMAILLISTPENDINGTAB',
        content: state.emailListPendingTab,
        type: 'session'
      })
    },
    //未读邮件
    SET_EMAILLISTUNREADTAB(state, param) {//type,list,id
      if (param.type == 1) {//插入数组
        state.emailListUnreadTab.list.push(param.row);
        state.emailListUnreadTab.current = param.row.id;
      } else if (param.type == 0) {//保存当前高亮
        state.emailListUnreadTab.current = param.current;
      } else if (param.type == 2) {//删除一个
        state.emailListUnreadTab.list = param.list;
        state.emailListUnreadTab.current = param.list[param.index - 1].id
      }
      setStore({
        name: 'EMAILLISTUNREADTAB',
        content: state.emailListUnreadTab,
        type: 'session'
      })
    },
    //已发送
    SET_EMAILLISTOUTTAB(state, param) {//type,list,id
      if (param.type == 1) {//插入数组
        state.emailListOutTab.list.push(param.row);
        state.emailListOutTab.current = param.row.id;
      } else if (param.type == 0) {//保存当前高亮
        state.emailListOutTab.current = param.current;
      } else if (param.type == 2) {//删除一个
        state.emailListOutTab.list = param.list;
        state.emailListOutTab.current = param.list[param.index - 1].id
      }
      setStore({
        name: 'EMAILLISTOUTTAB',
        content: state.emailListOutTab,
        type: 'session'
      })
    },
    //客户模式- 邮件列表
    SET_EMAILLISTCUSTOMERTAB(state, param) {//type,list,id
      if (param.type == 1) {//插入数组
        state.emailListCustomerTab.list.push(param.row);
        state.emailListCustomerTab.current = param.row.id;
      } else if (param.type == 0) {//保存当前高亮
        state.emailListCustomerTab.current = param.current;
      } else if (param.type == 2) {//删除一个
        state.emailListCustomerTab.list = param.list;
        state.emailListCustomerTab.current = param.list[param.index - 1].id
      }
      setStore({
        name: 'EMAILLISTCUSTOMERTAB',
        content: state.emailListCustomerTab,
        type: 'session'
      })
    },
    //下属模式- 邮件列表
    SET_EMAILLISTNEXTTAB(state, param) {//type,list,id
      if (param.type == 1) {//插入数组
        state.emailListNextTab.list.push(param.row);
        state.emailListNextTab.current = param.row.id;
      } else if (param.type == 0) {//保存当前高亮
        state.emailListNextTab.current = param.current;
      } else if (param.type == 2) {//删除一个
        state.emailListNextTab.list = param.list;
        state.emailListNextTab.current = param.list[param.index - 1].id
      } else if (param.type == 3) {
        state.emailListNextTab = {
          list: [{
            id: '0',
            title: '下属邮件列表',
            state: 0,
            close: false
          }],
          current: '0'
        }
      }
      setStore({
        name: 'EMAILLISTNEXTTAB',
        content: state.emailListNextTab,
        type: 'session'
      })
    },
    SET_PERCENT(state, current) {
      let arr = [];
      let target = JSON.parse(JSON.stringify(state.percent))
      let len = target.length, index = -1;
      if (len > 0) {
        for (var i = 0; i < len; i++) {
          let item = target[i];
          if (item.name == current.name) {
            index = i;
          }
        }
      } else {
        arr.push(current)
      }
      if (index != -1) {//新增的
        target[index].percent = current.percent;
      } else {
        target.push(current)
      }
      let arr1 = [];
      for (let item of target) {
        if (item.percent != 0) {
          arr1.push(item)
        }
      }
      state.percent = arr1;
    },
    RESET_PERCENT(state){
      state.percent = [];
    },
    SET_SOCIALFRAG(state, type) {
      state.socialFrag = type;
      setStore({
        name: 'SOCIALFRAG',
        content: state.socialFrag,
        type: 'session'
      })
    },
    SET_APPROVALEXAMSTATE(state, type) {
      state.approvalExamState = type;
      setStore({
        name: 'APPROVALEXAMSTATE',
        content: state.approvalExamState,
        type: 'session'
      })
    },
    SET_APPROVALSTATE(state, type) {
      state.approvalState = type;
      setStore({
        name: 'APPROVALSTATE',
        content: state.approvalState,
        type: 'session'
      })
    },
    SET_MAINFRAG(state, frag) {
      state.emailMainFrag = frag;
      setStore({
        name: 'EMAILMAINFRAG',
        content: state.emailMainFrag,
        type: 'session'
      })
    },
    SET_SIGNLIST: (state, params) => {
      state.sign = params;
      setStore({
        name: 'SIGNLIST',
        content: state.sign,
        type: 'session'
      })
    },
    SET_COMMONPAGE: (state, params) => {
      state.commonPage = params.page;
      state.commonPageSize = params.pageSize;
      setStore({
        name: 'COMMONPAGE',
        content: state.commonPage,
        type: 'session'
      })
      setStore({
        name: 'COMMONPAGESIZE',
        content: state.commonPageSize,
        type: 'session'
      })
    },
    SET_EMAILPAGE: (state, params) => {
      state.emailPage = params;
      setStore({
        name: 'EMAILPAGE',
        content: state.emailPage,
        type: 'session'
      })
    },
    SET_EMAILPAGESIZE: (state, params) => {
      state.emailPageSize = params;
      setStore({
        name: 'EMAILPAGESIZE',
        content: state.emailPageSize,
        type: 'session'
      })
    },
    SET_FIRSTEMAIL: (state, params) => {
      state.isFirstEmail = params;
      setStore({
        name: 'FIRSTEMAIL',
        content: state.isFirstEmail,
        type: 'session'
      })
    },
    SET_FIRSTSOCIAL: (state, params) => {
      state.isFirstSocial = params;
      setStore({
        name: 'FIRSTSOCIAL',
        content: state.isFirstSocial,
        type: 'session'
      })
    },
    SET_CURRENTSETFRAG: (state, params) => {
      state.currentSetFrag = params;
      setStore({
        name: 'CURRENTSETFRAG',
        content: state.currentSetFrag,
        type: 'session'
      })
    },
    SET_ISFRAG: (state, params) => {
      state.isFrag = params;
      setStore({
        name: 'ISFRAG',
        content: state.isFrag,
        type: 'session'
      })
    },
    SET_LABELID: (state, params) => {
      state.labelID = params;
      setStore({
        name: 'LABELID',
        content: state.labelID,
        type: 'session'
      })
    },
    SET_CURRENCYUNITS: (state, params) => {
      state.currencyUnits = params;
      setStore({
        name: 'CURRENCYUNITS',
        content: state.currencyUnits,
        type: 'session'
      })
    },
    SET_UNITS: (state, info) => {
      state.commonUnits = info;
      setStore({
        name: 'COMMONUNITS',
        content: state.commonUnits,
        type: 'session'
      })
    },
    SET_LABELLIST: (state, value) => {
      state.labelList = value;
      //合并标签数据
      let children = [];
      value.map(item => {
        children.push({
          value: item.id,
          label: item.labelName,
          labelColor: item.labelColor
        })
      })

      children.unshift({
        value: '-1',
        label: '已读',
      }, {
        value: '-2',
        label: '全部设为已读',
      },)
      children.push({
        value: '-3',
        label: '+新建标签并标记'
      })
      state.labelListTag = children;
      setStore({
        name: 'LABELLIST',
        content: state.labelList,
        type: 'session'
      })
      setStore({
        name: 'LABELLISTTAG',
        content: state.labelListTag,
        type: 'session'
      })
    },
    SAVECURRENTEMAIL: (state, value) => {
      state.currentEmail = value;
      setStore({
        name: 'CURRENTEMAIL',
        content: state.currentEmail,
        type: 'session'
      })
    },
    SET_ACTIVEDEFAULT: (state, value) => {
      state.defaultActive = value;
      setStore({
        name: 'ACTIVEDEFAULT',
        content: state.defaultActive,
        type: 'session'
      })
    },
    SET_FULLSCREN: (state) => {
      state.isFullScren = !state.isFullScren;
    },
    SET_EMAILLIST: (state, value) => {//保存邮箱列表信息
      state.emailList = value;
      setStore({
        name: 'EMAILLIST',
        content: state.emailList,
        type: 'session'
      })
    },
    SET_MESSAGElIST: (state, value) => {// 未读消息列表
      state.messageList = value;
      setStore({
        name: 'MESSAGElIST',
        content: state.messageList,
        type: 'session'
      })
    },
    // 右下角
    SET_NEWCOUNT: (state, value) => { // 右下角消息集合
      state.newCount.mail = value.newMailCount;
      state.newCount.system = value.newSystemCount;
      state.newCount.audit = value.auditCount;
      state.newCount.custimp = value.custimpCount;
      setStore({
        name: 'NEW_COUNT',
        content: state.newCount,
        type: 'session'
      })
    },
    SET_TOTAL: (state, value) => {// 未读消息总数
      state.total = value;
      setStore({
        name: 'TOAL',
        content: state.total,
        type: 'session'
      })
    },
    SET_TURNOVER: (state, value) => {
      state.turnoverOptions = value;
      setStore({
        name: 'TURNOVER_OPTIONS',
        content: state.turnoverOptions,
        type: 'session'
      })
    },
    SET_COMPANY_TYPE: (state, value) => {
      state.companyTypeOptions = value;
      setStore({
        name: 'COMPANY_TYPE_OPTIONS',
        content: state.companyTypeOptions,
        type: 'session'
      })
    },
    SET_INDUSTRY: (state, value) => {
      state.industryOptions = value;
      setStore({
        name: 'INDUSTRY_OPTIONS',
        content: state.industryOptions,
        type: 'session'
      })
    },
    SET_SOCIAL: (state, value) => {
      state.socialOptions = value;
      setStore({
        name: 'SOCIAL_OPTIONS',
        content: state.socialOptions,
        type: 'session'
      })
    },
    SET_EMPLE: (state, value) => {
      state.empleOptions = value;
      setStore({
        name: 'EMPLE_OPTIONS',
        content: state.empleOptions,
        type: 'session'
      })
    },
    SET_COUNTRY: (state, value) => {
      state.countryOptions = value;
      setStore({
        name: 'COUNTRY_OPTIONS',
        content: state.countryOptions,
        type: 'session'
      })
    }
  }
}
export default common
